<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
<!DOCTYPE html >
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>tailor</title>
    <style type="text/css">
        #sp ul li {
            float: left;
            padding-left: 20px;
            padding-bottom: 10px;
            list-style: none;
        }
    </style>
    <link rel="stylesheet" type="text/css"
          href="easyui/themes/bootstrap/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
    <script type="text/javascript" src="myjs/path.js"></script>
    <script type="text/javascript" src="easyui/jquery.min.js"></script>
    <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="myjs/tailor.js"></script>
    <script src="js/yangshi.js"></script>

</head>
<body>
<shiro:lacksPermission name="cj:look">没有操作裁剪模块的权限。。。</shiro:lacksPermission>
<shiro:hasPermission name="cj:look">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'north',split:true,border:false"
             style="height: 60%; width: 100%">
            <table id="dg">
            </table>
        </div>
        <div data-options="region:'center',border:false"
             style="height: 40%; width: 100%">
            <table id="dgChildren">
            </table>
        </div>
    </div>
    <div id="tbb" style="padding: 5px; height: auto">
        <span>详细信息 | </span> <label><input type='radio' name='tj' class="fenlei"
                                           onclick='loadChildrenList()' value='2'><span>按尺码</span></label> <label><input
            type='radio' onclick='loadChildrenList()' name='tj' value='1' class="fenlei"><span>按颜色</span></label>
        <label><input type='radio' onclick='loadChildrenList()'
                      checked='checked' name='tj' value='0' class="fenlei">按款号</label>
    </div>
    <div id="tb" style="padding: 5px; height: auto">
        <shiro:hasPermission name="cj:create">
            <a id='xinzeng' href="#" class="easyui-linkbutton"
               iconCls="icon-add" plain="true" onclick="winOpen(0)">新增单</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="cj:update">
            <a id='xiugai' href="javascript:void(0)" class="easyui-linkbutton"
               data-options="iconCls:'icon-edit',plain:true" onclick="winOpen(1)">修改单</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="cj:del">
            <a href="javascript:void(0)" class="easyui-linkbutton"
               data-options="iconCls:'icon-remove',plain:true" onclick="shanchu()">删除</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="cj:cbj">
            <a href="#" class="easyui-linkbutton"
               data-options="plain:true,iconCls:'icon-edit'" onclick="chengbenjia()">修改成本价</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="cj:qrk">
				<span title="请查看每单入库情况,如已全入库点击此项" class="easyui-tooltip" data-options="position: 'top'">
                <a href="#" id="cjfish" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="overOrder()">已全入库</a></span>
        </shiro:hasPermission>

        <shiro:hasPermission name="cj:diaopai">|
            <a id='huifu1' href="#" class="easyui-linkbutton" iconCls="icon-tip" onclick="opendp()"
               plain="true">打印吊牌</a>
        </shiro:hasPermission>
        | 日期从: <input id="startTime" class="easyui-datebox" editable='false'
                      style="width: 120px"> 到:
        <input id="endTime" class="easyui-datebox" editable='false' style="width: 120px">
        <a href="javascript:void(0)" class="easyui-linkbutton"
           data-options="iconCls:'icon-reload',plain:true" onclick="congzhi()">重置时间</a>
        贴牌：<input id="tiepai">
        <input id="product" class="easyui-searchbox" style="width: 200px"
               data-options="searcher:qq,prompt:'输入款号进行搜索',menu:'#mm'">

        <script type="text/javascript">
            function congzhi() {
                $('#startTime').combo('setText', '');
                $('#endTime').combo('setText', '');
                $('#key').searchbox('setValue', '');
                $('#startTime').combo('setValue', '');
                $('#endTime').combo('setValue', '');
                load();
            }
        </script>

        <div id="mm">
            <div data-options="name:'-1'">全部</div>
            <div data-options="name:'0'">新单</div>
            <div data-options="name:'1'">进行</div>
            <div data-options="name:'2'">完成</div>
        </div>
        <script>
            function qq(value, name) {
                //value是搜索框里的值,name是选择框里的值
                var startTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                // 判断日期有效性
                startDate = startTime.replace("-", "/");//替换字符，变成标准格式
                endDate = endTime.replace("-", "/");//替换字符，变成标准格式
                var d1 = new Date(Date.parse(startDate));
                var d2 = new Date(Date.parse(endDate));
                if (d1 > d2) {
                    $.messager.alert('提示', '开始日期不能大于结束日期');
                    return;
                }
                var tiepai = $("#tiepai").combobox("getValue");
                if (tiepai == "全部") {
                    tiepai = "";
                }
                $("#dg").datagrid({
                    url: path + '/tailor/list.do',
                    queryParams: {
                        state: name,
                        product: value.trim(),
                        startTime: startTime,
                        endTime: endTime,
                        tiepai: tiepai
                    }
                })
                ;
            }
        </script>
    </div>

    <div id="w" class="easyui-window"
         data-options="buttons:'#bb', modal:true,closed:true,collapsible:false,iconCls:'icon-save',minimizable:false,maximizable:false"
         style="width: 830px; height: 560px;">

        <table style="width: 100%; height: 30%; margin: 0 auto;margin-bottom: 20px">
            <tr>
                <th colspan="6" style="text-align: center"><h2>裁
                    剪 单</h2> <input id='id' type='hidden'>
            </tr>

            <tr>
                <td>裁剪单号：<input class="textbox"
                                id="caijiandanhao"
                                disabled="disabled" style="width: 130px; height: 20px"></td>
                <td>裁剪人：<input class="easyui-validatebox textbox"
                               data-options="required:false,validType:['length[0,20]']"
                               id="caijianren" style="width: 130px; height: 20px"></td>
                <td>贴牌：<input id="tiepais" style="width: 130px;"></td>
            </tr>
        </table>

        <div id="tb3">
            <span title="注：切换款号将清空已编辑数据" class="easyui-tooltip"
                  data-options="position: 'top'">商品款号：<input id="productCombobox" style="width: 140px;"></span>
            <a href="javascript:void(0)" class="easyui-linkbutton"
               data-options="iconCls:'icon-reload',plain:true" onclick="loadProduct()">刷新</a>

            <select id="cc" style="width: 140px;"></select>

            <div id="sp"></div>
            <script type="text/javascript">
                $(document).keyup(function (event) {
                    if (event.shiftKey && event.keyCode == 13) {
                        accept()
                    }
                    if (event.ctrlKey && event.keyCode == 13) {
                        $('#save').trigger('click')
                    }
                })
                function diaoyong(tar) {
                    if (event.shiftKey) {
                        $(tar).parents('tr').next().find('input').eq(0).blur();
                        return
                    }
                    if (event.keyCode == 13) {
                        if (flg) {
                            //取到当前填写的数
                            var shuliang = $(tar).val() - 0;
                            //数量
                            var count = $(tar).parents('td').siblings('td').find('input').length
                            for (var i = 0; i < count - 1; i++) {
                                $(tar).parents('td').siblings('td').find('input').eq(i).val(shuliang)
                            }
                            $(tar).parents('td').siblings('td').find('input').eq(count - 1).val(shuliang * count)
                            $(tar).parents('tr').next().find('input').eq(0).focus();
                        }
                        flg = true
                    }

                }

                //小计
                function diaoyong1(tar) {
                    if (event.shiftKey) {
                        $(tar).parents('tr').next().find('input').eq(0).blur();
                        return
                    }
                    if (event.keyCode == 13) {
                        if (flg) {
                            var count = $(tar).parents('td').siblings('td').find('input').length
                            var zongji = $(tar).val() - 0;
                            //得到这个数量
                            var sl = zongji / count
                            for (var i = 0; i < count; i++) {
                                $(tar).parents('td').siblings('td').find('input').eq(i).val(sl);
                            }
                            $(tar).parents('tr').next().find('input').eq(count).focus();
                        }
                        flg = true
                    }

                }


                var iq = 1;
                var flg = true;//解决enter键第一次冲突问题

                function A() {
                    var sum = 0;
                    if (iq < $("#sp table tr").length) {
                        var a = $("#sp table").find("tr").eq(iq).find("td").eq(1).children().val()
                        for (var j = 1; j < $("#sp table td").length / $("#sp table tr").length - 1; j++) {
                            /*  var v = $("#sp table tr:eq(0) td:eq(1)") */
                            if (a != '') {
                                if (/^[+]?[0-9]+\d*$/.test(a)) {
                                    $("#sp table").find("tr").eq(iq).find("td").eq(j).children().val(a)

                                } else {
                                    $("#sp table").find("tr").eq(iq).find("td").eq(1).children().focus()
                                    return
                                }


                                sum += parseInt($("#sp table").find("tr").eq(iq).find("td").eq(j).children().val())

                            }
                        }

                        $("#sp table").find("tr").eq(iq).find("td").eq($("#sp table td").length / $("#sp table tr").length - 1).children().val(sum)

                        if (iq == $("#sp table tr").length - 1) {
                            iq = 0
                            $("#sp table").find("tr").eq(1).find("td").eq(1).children().focus()
                        }

                    }

                    $("#sp table").find("tr").eq(iq + 1).find("td").eq(1).children().focus()
                    iq++

                }

                $(function () {
                    $('#cc').combo({
                        editable: false,
                        panelWidth: 500,
                        panelHeight: 240
                    });
                    $('#sp').appendTo($('#cc').combo('panel'));
                });
            </script>


            <span title="按住Shift+Enter进行快捷保存编辑" class="easyui-tooltip"
                  data-options="position: 'top'"><a href="javascript:void(0)"
                                                    class="easyui-linkbutton"
                                                    data-options="iconCls:'icon-save',plain:true"
                                                    onclick="accept()">保存编辑</a></span>
            <a href="javascript:void(0)" class="easyui-linkbutton"
               data-options="iconCls:'icon-remove',plain:true"
               onclick="removeit()">删除新增</a>
            <div style="float: right">共<span id='zongjianshu'>0</span>件</div>
        </div>


        <div id="bb">
        <span title="按住Ctrl+Enter进行快捷保存" class="easyui-tooltip"
              data-options="position: 'top'"><button id="save"
                                                     onclick="save()" class="easyui-linkbutton"
                                                     data-options="iconCls:'icon-save'">保存</button></span>
            <button id="no" onclick="$('#w').window('close')" class="easyui-linkbutton"
                    data-options="iconCls:'icon-no'">取消
            </button>
        </div>
        <table id='dg3' class="easyui-datagrid" style="height: 60%"></table>
    </div>


    <div id="wchengbenjia" class="easyui-dialog"
         data-options="buttons:'#bss',modal:true,closed:true,collapsible:false,iconCls:'icon-edit',minimizable:false,maximizable:false,title:'修改成本价'"
         style="width: 250px;height: 140px;padding: 15px">
        <div style="margin-top: 7px;">
            <input id="caijianid" type="hidden">
            <span>成本价：</span><input class="easyui-numberbox" id="cbj"
                                    data-options="precision:2,min:0,prompt:'请输入成本价'"
                                    style="width: 130px;">
        </div>
        <div id="bss">
            <button class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="saveChengbenjia()">确认</button>
            <button onclick="$('#wchengbenjia').window('close')" class="easyui-linkbutton"
                    data-options="iconCls:'icon-no'">取消
            </button>
        </div>
    </div>
</shiro:hasPermission>
</body>
</html>
<script>

    $("#tiepai").combobox({
        url: path + '/tailor/tiepai.do',
        valueField: 'tiepai',
        textField: 'tiepai',
        panelHeight: 150,
        width: 150,
        loadFilter: function (data) {
            return [{tiepai: '全部'}].concat(data)
        },
        onHidePanel: function () {
            var valueField = $(this).combobox("options").valueField;
            var val = $(this).combobox("getValue");  //当前combobox的值
            var allData = $(this).combobox("getData");   //获取combobox所有数据
            var result = true;      //为true说明输入的值在下拉框数据中不存在
            for (var i = 0; i < allData.length; i++) {
                if (val == allData[i][valueField]) {
                    result = false;
                }
            }
            if (result) {
                $(this).combobox("clear");
            }
        },
        onLoadSuccess: function () { //默认选中第一条数据
            $('#tiepai').combobox('select', '全部');
        }
    })


    /*裁剪上面表格*/
    $("#dg").datagrid({
        method: 'GET',
        striped: true,
        rownumbers: false,
        singleSelect: true,
        toolbar: '#tb',
        checkbox: false,
        fit: true,
        striped: true,
        fitColumns: false,
        autoRowHeight: false,
        rownumbers: true,
        pagination: true,
        pageSize: 15,
        pageList: [15, 30, 50],
        nowrap: true,
        loadMsg: '加载中,请稍后......',
        columns: [[{
            field: 'id',
            title: '主键',
            width: 80,
            hidden: true
        }, {
            field: 'caijiandanhao',
            title: '裁剪单号',
            width: 130,
            align: 'center'
        }, {
            field: 'product',
            title: '款号',
            width: 130,
            align: 'center'
        }, {
            field: 'state',
            title: '状态',
            width: 90,
            align: 'center',
            styler: function (value, row, index) {
                if (row.state == 0) {
                    return 'color:red;'
                } else if (row.state == 1) {
                    return 'color:orange'
                } else {
                    return 'color:green'
                }
            },
            formatter: function (value, row, index) {
                if (value == 0) {
                    return '新单'
                } else if (value == 1) {
                    return '进行'
                } else if (value == 2) {
                    return '完成'
                }
            }
        }, {
            field: 'chengbenjia',
            title: '成本价',
            width: 130,
            align: 'right',
            formatter: function (value, row, index) {
                if (value != null) {
                    return value.toFixed(2);
                } else {
                    return 0;
                }
            }
        }, {
            field: 'caijianshuliang',
            title: '裁剪总数',
            width: 90,
            align: 'center'
        }, {
            field: 'baosun',
            title: '报损总数',
            width: 90,
            align: 'center',
            formatter: function (value, row, index) {
                if (row.state != 2) {
                    return 0;
                } else {
                    return row.caijianshuliang - row.yiruku;
                }
            }
        }, {
            field: 'yiruku',
            title: '已入库数',
            width: 90,
            align: 'center'
        }, {
            field: 'dairuku',
            title: '待入库数',
            width: 90,
            align: 'center',
            formatter: function (value, row, index) {
                if (row.state != 2) {
                    return row.caijianshuliang - row.yiruku;
                } else {
                    return 0;
                }
            }
        }, {
            field: 'userName',
            title: '制单人',
            width: 110,
            align: 'center'
        }, {
            field: 'caijianren',
            title: '裁剪人',
            width: 150,
            align: 'center'
        }, {
            field: 'tiepai',
            title: '贴牌',
            width: 110,
            align: 'center'
        }, {
            field: 'createTime',
            title: '制单日期',
            width: 160,
            align: 'center'
        }
        ]],
        onSelect: function (index, row) {
            $("#dgChildren").datagrid({
                url: path + '/tailor/childrenlist/id.do',
                queryParams: {
                    id: row.id,
                    fenlei: $(".fenlei:checked").val()
                }
            });
        },
        onBeforeLoad: function (param) {
        },
        onLoadSuccess: function (data) {
            if (data.total > 0) {
                $('#dg').datagrid("selectRow", 0);
            } else {
                $("#dgChildren").datagrid("loadData", {rows: []});
            }
        },
        onLoadError: function () {
            //	console.debug('not ok')
        }
    });

    function loadChildrenList() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $("#dgChildren").datagrid({
                url: path + '/tailor/childrenlist/id.do',
                queryParams: {
                    id: row.id,
                    fenlei: $(".fenlei:checked").val()
                }
            });
        } else {
            $.messager.alert('提示', '请选中要查看的数据');
        }
    }

    /*裁剪单明细*/
    $("#dgChildren").datagrid({
        method: 'GET',
        striped: true,
        rownumbers: false,
        singleSelect: true,
        toolbar: '#tbb',
        checkbox: false,
        fit: true,
        striped: true,
        fitColumns: false,
        autoRowHeight: false,
        rownumbers: true,
        pagination: false,
        nowrap: true,
        loadMsg: '加载中,请稍后......',
        columns: [[{
            field: 'id',
            title: '主键',
            width: 80,
            hidden: true
        }, {
            field: 'state',
            title: '主键',
            width: 80,
            hidden: true
        }, {
            field: 'product',
            title: '款号',
            width: 130,
            align: 'center'
        }, {
            field: 'productName',
            title: '商品名称',
            width: 90,
            align: 'center'
        }, {
            field: 'mianliao',
            title: '面料',
            width: 180,
            align: 'center'
        }, {
            field: 'color',
            title: '颜色',
            width: 90,
            align: 'center'
        }, {
            field: 'size',
            title: '尺码',
            width: 90,
            align: 'center'
        }, {
            field: 'caijianshuliang',
            title: '裁剪数',
            width: 90,
            align: 'center'
        }, {
            field: 'baosun',
            title: '报损总数',
            width: 90,
            align: 'center',
            formatter: function (value, row, index) {
                if (row.state != 2) {
                    return 0;
                } else {
                    return row.caijianshuliang - row.yiruku;
                }
            }
        }, {
            field: 'yiruku',
            title: '已入库数',
            width: 90,
            align: 'center'
        }, {
            field: 'dairuku',
            title: '待入库数',
            width: 90,
            align: 'center',
            formatter: function (value, row, index) {
                if (row.state != 2) {
                    return row.caijianshuliang - row.yiruku;
                } else {
                    return 0;
                }
            }
        }
        ]],
        onSelect: function (index, row) {

        },
        onBeforeLoad: function (param) {
        },
        onLoadSuccess: function (data) {
            var fenlei = $(".fenlei:checked").val();
            if (fenlei == 0) {
                $("#dgChildren").datagrid('hideColumn', 'color');
                $("#dgChildren").datagrid('hideColumn', 'size');
            } else if (fenlei == 1) {
                $("#dgChildren").datagrid('showColumn', 'color');
                $("#dgChildren").datagrid('hideColumn', 'size');
            } else {
                $("#dgChildren").datagrid('showColumn', 'color');
                $("#dgChildren").datagrid('showColumn', 'size');
            }
        },
        onLoadError: function () {
            //	console.debug('not ok')
        }
    });


    //修改新增的表格
    $("#dg3").datagrid({
        method: 'GET',
        striped: true,
        rownumbers: false,
        singleSelect: false,
        toolbar: '#tb3',
        checkbox: false,
        fit: false,
        striped: true,
        fitColumns: false,
        autoRowHeight: false,
        rownumbers: true,
        pagination: false,
        nowrap: true,
        loadMsg: '加载中,请稍后......',
        onDblClickRow: onClickRow,
        columns: [[{
            field: 'id',
            title: '主键',
            width: 80,
            hidden: true
        }, {
            field: 'product',
            title: '款号',
            width: 130,
            align: 'center'
        }, {
            field: 'productName',
            title: '商品名称',
            width: 90,
            align: 'center'
        }, {
            field: 'color',
            title: '颜色',
            width: 90,
            align: 'center'
        }, {
            field: 'size',
            title: '尺码',
            width: 90,
            align: 'center'
        }, {
            field: 'caijianshuliang',
            title: '裁剪数',
            width: 90,
            align: 'center',
            editor: {
                type: 'validatebox',
                options: {
                    required: true,
                    validType: 'INT'
                }
            }
        }, {
            field: 'style',
            title: '商品款型',
            width: 90,
            align: 'center'
        }, {
            field: 'pinpai',
            title: '商品品牌',
            width: 90,
            align: 'center'
        }
        ]]
    });
    var f = false;
    function onClickRow(index) {
        if (editIndex != index) {
            // 点击修改总价
            if (f) {
                $('#dg3').datagrid('endEdit', editIndex);
                var drow1 = $('#dg3').datagrid('getData').rows[editIndex];
                var i = editIndex
                $('#dg3').datagrid('beginEdit', editIndex);
                zongjia()
            }
            if (endEditing()) {
                $('#dg3').datagrid('clearSelections');
                $('#dg3').datagrid('selectRow', index).datagrid('beginEdit', index);
                f = true
                editIndex = index;
            } else {
                $('#dg3').datagrid('selectRow', editIndex);
            }
        }
    }
</script>